import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../tool/assets.dart';
import 'search_row.dart';

class SearchView extends BLESearchDeviceDialog implements BLESearchDeviceDialogDelegate {
  SearchView({
    super.key,
    super.height,
  }) : super() {
    delegate = this;
  }
  @override
  Color? get contentColor => Color_bg_view;

  @override
  Widget? bgViewForFooter(Widget child) => null;

  @override
  Widget? bgViewForHeader(Widget child) => null;

  @override
  List<String>? filterName() => ['QHM'];

  @override
  List<RowModel> listModelForFooter(String appVersion) {
    var list = isGroupManager ? [assets.add_plus, assets.back] : ['Group Management', 'Version: $appVersion'];
    return list
        .map(($0) => RowModel(
            text: $0,
            textStyle: const TextStyle(fontSize: 14, color: Colors.white),
            height: isGroupManager ? 60 : 40,
            bgColor: Colors.transparent,
            lineColor: Color_line_view,
            lineHeight: 1,
            iconName: $0,
            iconSize: 25))
        .toList();
  }

  @override
  List<RowModel> listModelForHeader(String appVersion) {
    return ['Ungrouped Devices']
        .map(($0) => RowModel(
            text: $0,
            height: 60,
            textStyle: const TextStyle(fontSize: 20, color: Colors.white),
            lineColor: Color_line_view,
            lineHeight: 1,
            bgColor: Colors.transparent))
        .toList();
  }

  @override
  List<RowModel> listModelForSection(List<String> list) {
    return (list)
        .map(($0) => RowModel(
            text: $0,
            height: 40,
            textStyle: const TextStyle(fontSize: 14, color: Colors.white),
            lineColor: Color_line_view,
            lineHeight: 1,
            bgColor: Colors.transparent,
            iconName: assets.delete,
            iconSize: 20))
        .toList();
  }

  @override
  void methodForClickBLESearchTableView(BLESearchTableViewClickType type, String tag) {
    if (type == BLESearchTableViewClickType.footer) {
      switch (tag) {
        case 'Group Management':
          setState(() => isGroupManager = true);
        case assets.add_plus:
          var input = InputDialog(
              title: 'New Group',
              contentColor: Color_bg_view,
              textColor: Colors.white,
              lineColor: Color_line_view,
              style: InputDialogStyle.okAndCancel);
          input.show(state.context);
          input.onTap = (tag, content) {
            if (tag == 'OK') {
              addGroup(content);
            }
            print('==============>  修改名称点击  ${tag}');
            return true;
          };
        case assets.back:
          setState(() => isGroupManager = false);
      }
    }
  }

  @override
  void refreshMethodExentesion() {}

  @override
  Widget viewForRowInSection(DSPeripheral peripheral, int section, int row, _) {
    return SearchItem(
      device: peripheral,
      isManager: isGroupManager,
      onSelect: () => clickDevice(section, row),
      onPressLongToChangeName: () => changeDeviceName(section, row, peripheral.peripheral, null),
    );
  }
}
